package org.cpar.compiler;

import java.util.HashMap;

public class CPARContext {

	private final CPARContext parentContext;
	private final HashMap<String, CPARContextData> datas;
	private String name;
	private int itemIndex;
	private CPARNode cparNode;

	public CPARContext(String name, CPARNode node, CPARContext parent) {
		itemIndex = 0;
		cparNode = node;
		this.name = name;
		parentContext = parent;
		datas = new HashMap<String, CPARContextData>();
	}

	public CPARNode getCPARNode() {
		return cparNode;
	}

	public void setCPARNode(CPARNode v) {
		cparNode = v;
	}

	public int acquireItemIndex() {
		return ++itemIndex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public CPARContext getParentContext() {
		return parentContext;
	}

	public HashMap<String, CPARContextData> getDatas() {
		return datas;
	}

	public void addData(String name, Object data) {
		datas.put(name, new CPARContextData(name, data, this));
	}

	public boolean isGlobal() {
		return name == null;
	}

	public String getStackDataName() {
		if (name == null)
			return "CPARGlobalStackData";

		return "CPARTaskStackData_" + name;
	}

	public String getStackVarName() {
		if (name == null)
			return "pGlobalStack";
		return "pStack";
	}

	public String getContextVarName() {
		if (name == null)
			return "pGlobalContext";
		return "pContext";
	}

	public String getAllocatorCall() {
		return "CPARContextAllocatorGet(" + getContextVarName() + ")";
	}

	public String getFunctionName() {
		if (name == null)
			return "";

		return "CPARTask_" + name;
	}
}
